<?php

namespace app\modules\Manage\controllers;

use Yii;
use app\modules\Manage\models\User;
use app\modules\Manage\models\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * UserController implements the CRUD actions for User model.
 */
class UserController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all User models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new UserSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single User model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new User model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new User();

        if ($model->load(Yii::$app->request->post())) {


            $model->password_hash = Yii::$app->security->generatePasswordHash($model->password);
            $model->auth_key = Yii::$app->security->generateRandomString();

            $model->company = json_encode($model->company);
            
            $model->manage_user_id = json_encode($model->manage_user_id);

            $model->status = 10;

            $model->created_at  = time();
            $model->updated_at = time();

            if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
            }

        }
        $model->nation = 1;
        return $this->render('create', [
            'model' => $model,
        ]);
    }

   
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {

            if($model->password){
                
               $model->password_hash = Yii::$app->security->generatePasswordHash($model->password);
            }
            
            $model->company = json_encode($model->company);

            $model->manage_user_id = json_encode($model->manage_user_id);

            $model->updated_at = time();

           
            if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
            }else{
                 print_r($model->errors);exit();
            }
            
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    

    /**
     * Finds the User model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return User the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = User::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}
